package io.github.talelin.latticy.service.impl;

import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.GridCategoryDTO;
import io.github.talelin.latticy.mapper.CategoryMapper;
import io.github.talelin.latticy.model.CategoryDO;
import io.github.talelin.latticy.model.GridCategoryDO;
import io.github.talelin.latticy.mapper.GridCategoryMapper;
import io.github.talelin.latticy.service.GridCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-06-20
 */
@Service
public class GridCategoryServiceImpl extends ServiceImpl<GridCategoryMapper, GridCategoryDO> implements GridCategoryService {

    @Value("${sleeve.grid-category-maximum-quantity}")
    private Integer maximumQuality;

    @Autowired
    private GridCategoryMapper gridCategoryMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void save(GridCategoryDTO gridCategoryDTO) {
        Integer count = this.getBaseMapper().selectCount(null);
        if (count > maximumQuality) {
            throw new ForbiddenException(50001);
        }


        CategoryDO categoryDO = categoryMapper.selectById(gridCategoryDTO.getCategoryId());
        if (categoryDO == null) {
            throw new NotFoundException(40000);
        }

        GridCategoryDO gridCategoryDO = new GridCategoryDO();
        BeanUtils.copyProperties(gridCategoryDTO, gridCategoryDO);

        this.save(gridCategoryDO);
    }

    @Override
    public void update(Long id, GridCategoryDTO gridCategoryDTO) {
        GridCategoryDO gridCategoryDO = this.getBaseMapper().selectById(id);
        if (gridCategoryDO == null) {
            throw new NotFoundException(40000);
        }
        CategoryDO categoryDO = this.categoryMapper.selectById(gridCategoryDTO.getCategoryId());
        if (categoryDO == null) {
            throw new NotFoundException(40000);
        }
        setGridCategoryByCondition(gridCategoryDTO, gridCategoryDO, categoryDO);
        this.updateById(gridCategoryDO);

    }

    @Override
    public void deleteGridCategory(Long id) {
        GridCategoryDO categoryDO = this.getById(id);
        if (categoryDO  == null){
            throw new NotFoundException(40000);
        }
        this.removeById(id);
    }


    public void deleteGridCategory(Integer id) {
        GridCategoryDO gridCategory = this.getById(id);
        if (gridCategory == null) {
            throw new NotFoundException(50000);
        }
        this.getBaseMapper().deleteById(id);
    }

    private void setGridCategoryByCondition(GridCategoryDTO gridCategoryDTO, GridCategoryDO gridCategoryDO, CategoryDO categoryDO) {
        // 如果存在 title，赋值 title，否则填充 name
        if (gridCategoryDTO.getTitle() == null) {
            gridCategoryDO.setTitle(categoryDO.getName());
        } else {
            gridCategoryDO.setTitle(gridCategoryDTO.getTitle());
        }

        if (gridCategoryDTO.getName() == null) {
            gridCategoryDO.setTitle(categoryDO.getName());
        } else {
            gridCategoryDO.setTitle(gridCategoryDTO.getName());
        }

        gridCategoryDO.setImg(gridCategoryDTO.getImg());
        // 如果当前绑定的分类无父分类，则绑定到rootCategoryId
        // 否则绑定父分类绑定到rootCategoryId，当前id绑定到categoryId
        if (categoryDO.getParentId() == null){
            gridCategoryDO.setRootCategoryId(gridCategoryDTO.getCategoryId());
        }else {
            gridCategoryDO.setRootCategoryId(gridCategoryDTO.getCategoryId());
            gridCategoryDO.setCategoryId(gridCategoryDTO.getCategoryId());
        }
    }

}
